import xml.dom.minidom as minidom  # xml读写


class XmlHelper(object):
    # 读取xml的元素
    @staticmethod
    def get_xml_elements(file_name):
        dom = minidom.parse(file_name)
        root = dom.documentElement
        strings = root.getElementsByTagName('string')
        return strings

    # 创建Xml的根
    @staticmethod
    def create_xml_dom(root_name):
        dom = minidom.getDOMImplementation().createDocument(None, root_name, None)
        return dom

    # 创建Xml的元素
    @staticmethod
    def create_xml_element(dom, attr_name, attr_desc, element_val):
        element = dom.createElement('string')
        element.appendChild(dom.createTextNode(element_val))
        element.setAttribute('name', attr_name)
        element.setAttribute('desc', attr_desc)
        return element

    # 写xml到文件
    @staticmethod
    def write_xml_file(file_name, dom):
        with open(file_name, 'w', encoding='utf-8') as f:
            dom.writexml(f, addindent='\t', newl='\n', encoding='utf-8')
